package com.bobby.web;

import com.bobby.pojo.User;
import com.bobby.service.UserService;
import com.bobby.service.impl.UserServiceImpl;
import com.bobby.utils.JdbcUtils;
import com.bobby.utils.WebUtils;
import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @Description TODO
 * @Date 2020/8/14 13:31
 * @Created wenjunpei
 */
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();


    protected void login(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String codeFromSession = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        String codeFromWeb = request.getParameter("code");
        if(codeFromSession == null ) {
            String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
            response.sendRedirect(root + "pages/user/login.jsp");
            return;
        }
        if(!codeFromSession.equalsIgnoreCase(codeFromWeb)) {
            String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
            response.sendRedirect(root + "pages/user/login.jsp");
            return;
        }

        User userFromWeb = WebUtils.copyParamToBean(request.getParameterMap(), User.class);

        User userFromDB = null;

        userFromDB = userService.login(userFromWeb);

        if(userFromDB == null ) {
            request.setAttribute("errorMsg","用户名或密码错误");
            request.setAttribute("username",userFromWeb.getUsername());
            request.setAttribute("password",userFromWeb.getPassword());
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }else {
            request.getSession().setAttribute("user",userFromDB);
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }
    }

    protected void regist(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String codeFromSession = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        String codeFromWeb = request.getParameter("code");

        User userFromWeb = WebUtils.copyParamToBean(request.getParameterMap(), User.class);

        request.setAttribute("username",userFromWeb.getUsername());
        request.setAttribute("password",userFromWeb.getPassword());
        request.setAttribute("email",userFromWeb.getEmail());
        if(codeFromSession != null && codeFromSession.equalsIgnoreCase(codeFromWeb)){

            boolean flag = userService.existsUsername(userFromWeb.getUsername());
            if(flag) {
                request.setAttribute("errorMsg","用户名已存在");
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
            }else {
                userService.registerUser(userFromWeb);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request,response);
            }

        }else {
            request.setAttribute("errorMsg","验证码错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
        }
    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().invalidate();
        String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
        response.sendRedirect(root );
    }

    protected void ajaxExistsUsername(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        boolean flag = userService.existsUsername(username);
        Map<String,Object> map = new HashMap<>();
        map.put("existsUsername",flag);
        Gson gson = new Gson();
        String mapJsonString = gson.toJson(map);
        response.getWriter().write(mapJsonString);
    }
}
